package yf.mall.controller.api.app;

import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.entity.DyAccountAuthEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.UserService;
import yf.mall.service.app.DyAccountAuthService;
import yf.mall.vo.app.AppUserInfoVo;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/app/mini/account/")
public class ApiAppAccountController {

    @Resource
    private UserService userService;

    @Resource
    private DyAccountAuthService dyAccountAuthService;

    @PostMapping("/info")
    public R update(@LoginUserId Long userId) {
        if (userId == null) {
            return R.notLogin();
        }

        UserEntity userEntity = userService.get(userId);

        AppUserInfoVo userInfoVo = new AppUserInfoVo();
        userInfoVo.setUserId(userId);
        userInfoVo.setAvatar(userEntity.getAvatar());
        userInfoVo.setId(userEntity.getId());
        userInfoVo.setIsBindMobile(StringUtils.isNotBlank(userEntity.getMobile()));
        userInfoVo.setLevel(userEntity.getLevelName());
        userInfoVo.setMobile(userEntity.getMobile());
        userInfoVo.setNickName(userEntity.getName());
        userInfoVo.setIsSetPassword(StringUtils.isNotBlank(userEntity.getPassword()));

        DyAccountAuthEntity dyAccountAuthEntity = dyAccountAuthService.getMasterAuthByUserId(userId);

        boolean isAuth = dyAccountAuthEntity != null;
        userInfoVo.setIsKolAuth(isAuth);
        userInfoVo.setIsKolOrder(isAuth);

        return R.ok(userInfoVo);
    }
}
